package com.jichangxiu.framework.security.entity;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

/**
 * AuthToken:
 *
 * @author Jcx
 * @create 2023-04-04 13:34
 */
public class AuthToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 1L;

    private final Object tenantName;

    private final Object principal;

    private Object credentials;

    public AuthToken(Object tenantName, Object principal, Object credentials) {
        super(null);
        this.tenantName = tenantName;
        this.principal = principal;
        this.credentials = credentials;
        this.setAuthenticated(false);
    }

    public AuthToken(Object tenant, Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.tenantName = tenant;
        this.principal = principal;
        this.credentials = credentials;
        super.setAuthenticated(true);
    }

    public Object getTenantName() {
        return this.tenantName;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public void setAuthenticated(boolean authenticated) throws IllegalArgumentException {
        Assert.isTrue(!authenticated, "令牌设置失败");
        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }

}
